  import React, { useState, useEffect } from 'react';

// 自定义 Hook
function useCount(initialCount) {
  const [count, setCount] = useState(initialCount);
  const [status ,setStatus] = useState(true);
  let timer = 0
  useEffect(() => {
    timer = setTimeout(() =>{
      setCount(count+1)
      return () => {
        clearTimeout(timer)
      }
    },1000)
  },[count])  

  const toggle = (() => {
    if(status){
      clearTimeout(timer)
      setStatus(false)
    }else{
      setCount(count+1)
      setStatus(true)
    }
  })

  return [count, setCount,toggle,status];
}

// 计数器组件
function Counter() {
  const [count, setCount,toggle,status] = useCount(0);
  return (
    <>
      <p>Count: {count}</p>
      <button onClick={() => setCount(count + 1)}>ADD</button>
      <button onClick={toggle}>{status ? '暂停' : '启动'}</button>
    </>
  );
}

export default Counter;
